import { ValueConverter } from '@angular/compiler/src/render3/view/template';
import { Component, OnInit } from '@angular/core';
import { StorageServiceService } from 'src/app/storage-service.service';
// 模仿京东的历史搜索页面，能够展示最近搜索的一些项目

@Component({
  selector: 'app-todolist',
  templateUrl: './todolist.component.html',
  styleUrls: ['./todolist.component.css']
})
export class TodolistComponent implements OnInit {

  constructor( public storage:StorageServiceService) { 
      
  // 调用我们的service中的方法
  // let s = this.storage.get();
  // console.log(s);
  
  
  }
  // keyWords
  public keyWords:string;
  //HistoryList
  public  todolist:any[]=[
    {
      title:'xxx',
      status: 0
    }
  ];
  ngOnInit() {
// 获取本地的todolist
  var todolist:any = this.storage.get('todolist');
  if(todolist)
  {
    // 数据持久化
  this.todolist = todolist;
  }

  }

  // 向todoList中添加数据
  doAdd(e){
    if(e.keyCode==13){
      if(!this.todolistHasKeyword(this.todolist,this.keyWords))
    {   this.todolist.push({
        title: this.keyWords,
        status:0
      })
      // 更新localstorage中的todolist
      this.storage.set("todolist",this.todolist);
    }
    else
    {
      alert("待办事件已存在");
      this.keyWords =''; 
      // 0表示代办事项，1表示已完成事项
      }
    }
  }
// 删除todoList数据
  deleteData(key){
    this.todolist.splice(key,1);
    // 重更新localstorage中的todolist
    this.storage.set("todolist",this.todolist);
  }

  // 设置为any类型，防止后续出现问题

  // 异步，每次都会返回一个false，
  todolistHasKeyword(todolist:any,keyword:any){
    todolist.forEach(value => {
      if(value.title == keyword){
      return true;
      }
      
    });
    return false;
  }

  checkboxChange(){
    console.log("checkBox事件已触发");
    
  }

}
